﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QFlow.Data;

namespace QFlow.Windows.Reports
{
    public partial class InventoryDetailsReportViewer : Utils.BaseForm
    {
        public InventoryDetailsReportViewer(QFlowDbContext context)
            : base(context)
        {
            InitializeComponent();
        }

        private void InventoryDetailsReportViewer_Load(object sender, EventArgs e)
        {
            cmbUsers.DataSource = this.Context.Users.ToArray();
            cmbUsers.SelectedIndex = -1;
            cmbProducts.DataSource = this.Context.Products.Where(product => !product.IsNonStockable);
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            int receivedItemTypeId = this.Context.ItemTypes.GetReceivedType().ItemTypeID;
            int closedStatusId = this.Context.OrderStatus.GetClosedStatus().StatusID;

            int productId = (int)cmbProducts.SelectedValue;
            int userId = (cmbUsers.SelectedIndex == -1 ? 0 : (int)cmbUsers.SelectedValue);

            DateTime fromDate = dtFrom.LockUpdateChecked ? dtFrom.Value.Date : DateTime.MinValue;
            DateTime toDate = dtTo.LockUpdateChecked ? dtTo.Value.Date : DateTime.MinValue;

            var purchased = from pOrder in this.Context.PurchaseOrders
                            join pItem in this.Context.PurchaseItems
                            on pOrder.PurchaseOrderID equals pItem.PurchaseOrderID

                            where
                            pOrder.StatusID == closedStatusId
                            &&
                            pItem.ItemTypeID == receivedItemTypeId
                            &&
                            pItem.ProductID == productId
                            &&
                            (userId == 0 ? true : pItem.UserID == userId)
                            &&
                            (!dtFrom.LockUpdateChecked ? true : pOrder.Date >= fromDate)
                            &&
                            (!dtTo.LockUpdateChecked ? true : pOrder.Date <= toDate)

                            select new InventoryDetailsReportResult
                            {
                                OrderNumber = pItem.PurchaseOrderID,
                                Date = pItem.Timestamp,
                                OrderType = QFlow.Windows.Properties.Resources.RPPurchased,
                                Quantity = pItem.Quantity,
                                UserName = pItem.User.Username,
                                Price = pItem.Quantity * pItem.UnitPrice
                            };

            var sold = from sOrder in this.Context.SaleOrders
                       join sItem in this.Context.SaleItems
                       on sOrder.SaleOrderID equals sItem.SalesOrderID

                       where
                       sOrder.StatusID == closedStatusId
                       &&
                       sItem.ItemTypeID == receivedItemTypeId
                       &&
                       sItem.ProductID == productId
                       &&
                       (userId == 0 ? true : sItem.UserID == userId)
                       &&
                       (!dtFrom.LockUpdateChecked ? true : sOrder.Date >= fromDate)
                       &&
                       (!dtTo.LockUpdateChecked ? true : sOrder.Date <= toDate)

                       select new InventoryDetailsReportResult
                       {
                           OrderNumber = sItem.SalesOrderID,
                           Date = sItem.Timestamp,
                           OrderType = QFlow.Windows.Properties.Resources.RPSales,
                           Quantity = sItem.Quantity,
                           UserName = sItem.User.Username,
                           Price = sItem.Quantity * sItem.UnitPrice
                       };


            var result = purchased.Union(sold).OrderBy(detail => detail.OrderNumber);
            InventoryDetailsReportResultBindingSource.DataSource = result.ToArray();

            List<Microsoft.Reporting.WinForms.ReportParameter> repeortParams = new List<Microsoft.Reporting.WinForms.ReportParameter>();
            repeortParams.Add(new Microsoft.Reporting.WinForms.ReportParameter("FromDate", (dtFrom.LockUpdateChecked ? fromDate.ToString("dd-MM-yyyy") : "All")));
            repeortParams.Add(new Microsoft.Reporting.WinForms.ReportParameter("ToDate", (dtTo.LockUpdateChecked ? toDate.ToString("dd-MM-yyyy") : "All")));
            repeortParams.Add(new Microsoft.Reporting.WinForms.ReportParameter("Product", cmbProducts.Text));
            repeortParams.Add(new Microsoft.Reporting.WinForms.ReportParameter("User", (userId != 0 ? cmbUsers.Text : "All")));

            reportViewer1.LocalReport.SetParameters(repeortParams);

            reportViewer1.RefreshReport();
        }
    }

    public class InventoryDetailsReportResult
    {
        public int OrderNumber { get; set; }
        public DateTime Date { get; set; }
        public double Quantity { get; set; }
        public string OrderType { get; set; }
        public double Price { get; set; }
        public string UserName { get; set; }
    }
}
